package com.example.jiwhsgs;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Random;

public class ZXactivity extends Activity {
    private String[] sp;
    private HashMap<Integer, Integer> buttonClickCountMap;
    private final int[] buttonIds = {R.id.button1, R.id.button2, R.id.button3, R.id.button4, R.id.button5};
    private int[] textViewIds = {R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4, R.id.textView5};
    private String[] str = new String[5];
    private TextView sssTextView;
    private Handler handler = new Handler();
    private Runnable countdownRunnable;
    private int countdownTime = 9; // 倒计时时间，单位秒

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 隐藏状态栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zx);

        sp = getResources().getStringArray(R.array.shouPai);
        initZX();

        // 设置监听事件
        for (int i = 0; i < buttonIds.length; i++) {
            Button button = findViewById(buttonIds[i]);
            setButtonClickListener(button, buttonIds[i]);
        }

        // 初始化 sssTextView
        sssTextView = findViewById(R.id.sss);
        startCountdown();
    }

    private void initZX() {
        // 生成1-36的五个随机数且不能重复
        int[] nums = generateUniqueRandomNumbers(5, 1, 36);
        for (int i = 0; i < 5; i++) {
            str[i] = sp[nums[i] - 1];
        }

        // buttonClickCountMap 计数，点击多少次
        buttonClickCountMap = new HashMap<>();
        for (int i = 0; i < 5; i++) {
            buttonClickCountMap.put(buttonIds[i], 0);

            // 给对应的id改变text
            Button button = findViewById(buttonIds[i]);
            button.setText(str[i]);
        }
    }

    private void setButtonClickListener(Button button, int buttonId) {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = buttonClickCountMap.getOrDefault(buttonId, 0) + 1;
                buttonClickCountMap.put(buttonId, count);

                // 更新对应的 TextView
                int which = 0;
                int textViewId = 0;
                if (buttonId == R.id.button1) {
                    textViewId = R.id.textView1;
                    which = 0;
                } else if (buttonId == R.id.button2) {
                    textViewId = R.id.textView2;
                    which = 1;
                } else if (buttonId == R.id.button3) {
                    textViewId = R.id.textView3;
                    which = 2;
                } else if (buttonId == R.id.button4) {
                    textViewId = R.id.textView4;
                    which = 3;
                } else if (buttonId == R.id.button5) {
                    textViewId = R.id.textView5;
                    which = 4;
                }
                TextView textView = findViewById(textViewId);
                if (count < 10)
                    textView.setText("Clicked " + count + " times");
                else {
                    // 让对应的按键不能按
                    button.setEnabled(false);
                    textView.setText(str[which]);
                    button.setBackgroundResource(R.drawable.sliver_button);
                }
            }
        });
    }

    private void startCountdown() {
        countdownRunnable = new Runnable() {
            @Override
            public void run() {
                if (countdownTime > 0) {
                    countdownTime--;
                    sssTextView.setText(String.valueOf(countdownTime));
                    handler.postDelayed(this, 1000); // 每秒执行一次
                } else {
                    disableAllButtons();
                }
            }
        };
        handler.post(countdownRunnable);
    }

    private void disableAllButtons() {
        for (int buttonId : buttonIds) {
            Button button = findViewById(buttonId);
            button.setEnabled(false);
            button.setBackgroundResource(R.drawable.sliver_button);
        }
    }

    private int[] generateUniqueRandomNumbers(int count, int min, int max) {
        if (max - min + 1 < count) {
            throw new IllegalArgumentException("Range is too small for the number of unique numbers requested.");
        }
        Random random = new Random();
        int[] numbers = new int[count];
        boolean[] used = new boolean[max - min + 1];

        for (int i = 0; i < count; i++) {
            int num;
            do {
                num = random.nextInt(max - min + 1) + min;
            } while (used[num - min]);
            used[num - min] = true;
            numbers[i] = num;
        }
        return numbers;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(countdownRunnable); // 移除倒计时任务
    }
}
